package com.lsl.stream;

import cn.hutool.core.collection.CollUtil;
import com.sun.imageio.plugins.common.I18N;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lishuailong
 * @create 2020-07-17 17:20
 */
public class test1 {
   public  static List<Employee> list;
    static {
        list=new ArrayList<>();
        list.add(new Employee("小明",23,9999.5));
        list.add(new Employee("小王",22,4000.5));
        list.add(new Employee("小芳",18,4900.5));
        list.add(new Employee("小雪",28, 3000.45));
        list.add(new Employee("小雪",28, 3000.45));
    }
    @Test
    public void test1(){
        System.out.println(list);
        //获得一个顺序流
        Stream<Employee> stream = list.stream();
        //获得一个并行流
        Stream<Employee> parallelStream = list.parallelStream();
    }

    @Test
    public void test2(){

        Stream<Employee> stream = list.stream();
        //1.查询年龄大于25的
        stream.filter(employee -> employee.getAge()>25).forEach(System.out::println);
        System.out.println();

        //2.跳过第一个
        stream = list.stream();
        stream.skip(1).forEach(System.out::println);
        System.out.println();

        //3.去除重复的
        stream = list.stream();
        stream.distinct().forEach(System.out::println);
        System.out.println();

        //2.limit第一个
        stream = list.stream();
        stream.limit(3).forEach(System.out::println);
        System.out.println();
    }

    /**
     * map练习1
     */
    @Test
    public void test3(){
        ArrayList<String> strings = CollUtil.toList(new String[]{"a", "b","c","d"});
        strings.stream().map(s -> s.toUpperCase()).forEach(System.out::println);
    }
    /**
     * map练习2
     */
    @Test
    public void test4(){
        ArrayList<String> strings = CollUtil.toList("abc", "abc", "abc", "abc");
        Stream<Stream<Character>> streamStream = strings.stream().map(o -> stringFrom(o));
        streamStream.forEach(o->{
            o.forEach(System.out::println);
        });

    }
    //将字符串中的多个字符构成的集合转换为对应的Stream的实例
    public static Stream<Character> stringFrom(String s){
        ArrayList<Character> list = new ArrayList<>();
        for (char c : s.toCharArray()) {
            list.add(c);
        }
        return list.stream();
    }
    /**
     * 排序
     */
    @Test
    public void test5(){
        ArrayList<Integer> strings = CollUtil.toList(5,7,15,1,3,45,6,5,28,6,89);
        strings.stream().sorted((i1,i2)->i1.compareTo(i2)).forEach(o-> System.out.println(o));
    }
    /**
     * 匹配与查找
     */
    @Test
    public void test6(){

        //查询是否所有的人都大于18岁
        boolean b = list.stream().allMatch(employee -> employee.getAge() > 18);
        System.out.println("b = " + b);

        //查询是否有人大于18岁
        boolean b1 = list.stream().anyMatch(employee -> employee.getAge() > 18);
        System.out.println("b1 = " + b1);

        //查询是否没有存在 小明这个人
        boolean b2 = list.stream().noneMatch(employee -> employee.getName().equals("小明"));
        System.out.println("b2 = " + b2);



    }

    /**
     * 归约
     */
    @Test
    public void test7(){
        ArrayList<Integer> strings = CollUtil.toList(5,7,15,1,3,45,6,5,28,6,89);
        Integer reduce = strings.stream().reduce(0, Integer::sum);
        System.out.println("reduce = " + reduce);

        //计算所有人的 工资总额

        Stream<Double> doubleStream = list.stream().map(Employee::getSalary);
        Optional<Double> reduce1 = doubleStream.reduce(Double::sum);

        System.out.println("reduce1 = " + reduce1);

    }

    /**
     * 收集
     */
    @Test
    public void test8(){

        //获得 年龄大于18的人
        List<Employee> collect = list.stream().filter(employee -> employee.getAge() > 18).collect(Collectors.toList());
        System.out.println(collect);

    }




}
